home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / ads / sample / gpalsym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  11.8 KB  |  367 lines

  1. /* Next available MSG number is  33 */
  2.  
  3. /***************************************************************************
  4.    Module Name:  gpalsym.c
  5.  
  6.    Copyright (C) 1992, 1993, 1994 by Autodesk, Inc.
  7.  
  8.    Permission to use, copy, modify, and distribute this software in 
  9.    object code form for any purpose and without fee is hereby granted, 
  10.    provided that the above copyright notice appears in all copies and 
  11.    that both that copyright notice and the limited warranty and 
  12.    restricted rights notice below appear in all supporting 
  13.    documentation.
  14.  
  15.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  16.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  17.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  18.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  19.    UNINTERRUPTED OR ERROR FREE.
  20.  
  21.    Use, duplication, or disclosure by the U.S. Government is subject to 
  22.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  23.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  24.    (Rights in Technical Data and Computer Software), as applicable.
  25.     
  26.    .
  27.  
  28.    Description:  ADS sample program which exercises the ads_getsym and
  29.                  ads_putsym functions.
  30.  
  31.    Author     :
  32.                  Autodesk, Inc.
  33.                  2320 Marinship Way
  34.                  Sausalito, CA. 94965
  35.                  (415)332-2344
  36.  
  37.     Function Entry Points:
  38.         void
  39.         main(argc, argv)
  40.         int    argc;              [Number of argument passed in cmd line]
  41.         char   *argv[];           [Array of pointers to arguments]
  42.  
  43.     Exported ADS Functions
  44.         TESTGPSYM                 [Test the ADS_XXXSYM functions]
  45.  
  46.     Modification History:
  47.         Feb 25 1992 - bcm - original creation
  48.  
  49.     Notes and restrictions on use:
  50.  
  51.  
  52. ***************************************************************************/
  53.  
  54. /**************************************************************************/
  55. /*  MODULE NAME  */
  56. /**************************************************************************/
  57. #define    GPALSYM
  58.  
  59. /****************************************************************************/
  60. /*  DEFINES  */
  61. /****************************************************************************/
  62. #define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
  63. #define SPOINT(p, x, y, z) p[X] = (x); p[Y] = (y); p[Z] = (z)
  64. #define CPOINT(dst, src) dst[X] = src[X]; dst[Y] = src[Y]; dst[Z] = src[Z]
  65.  
  66. /**************************************************************************/
  67. /*  TYPEDEFS  */
  68. /**************************************************************************/
  69. /* ADS Function Table */
  70. typedef struct {
  71.     char    *name;
  72.     int     (*fptr)();
  73. } ftblent;
  74.  
  75. typedef struct resbuf rbtype;
  76.  
  77. /**************************************************************************/
  78. /*  INCLUDES  */
  79. /**************************************************************************/
  80.  
  81. #include <stdio.h>
  82. #include "adslib.h"
  83. #include "ol_errno.h"
  84. /****************************************************************************/
  85. /*  LOCAL FUNCTION FORWARD DECLARATIONS  */
  86. /****************************************************************************/
  87. int     getsym();
  88. int     putsym();
  89.  
  90. /**************************************************************************/
  91. /*  GLOBAL VARIABLES  */
  92. /**************************************************************************/
  93. /* Table of ADS functions */
  94. ftblent exfun[] = {
  95.             {/*MSG0*/"GETSYM", getsym},
  96.             {/*MSG0*/"PUTSYM", putsym},
  97.         };
  98.  
  99. /**************************************************************************/
  100. /*  EXTERNAL FUNCTION DECLARATIONS  */
  101. /**************************************************************************/
  102.  
  103. /**************************************************************************/
  104. /*  EXTERNAL VARIABLE DECLARATIONS  */
  105. /**************************************************************************/
  106.  
  107. /****************************************************************************/
  108. /*  LOCAL FUNCTION DECLARATIONS  */
  109. /****************************************************************************/
  110. int geterrno   _((void));
  111. int funcload   _((void));
  112. int funcunload _((void));
  113. int dofun      _((void));
  114.  
  115. /******************************************************************************/
  116. /*.doc geterrno(internal) */
  117. /*+
  118.     This function is called to obtain the value of the AutoCAD system
  119.     variable ERRNO and return it as a result.
  120. -*/
  121. /******************************************************************************/
  122. int
  123. /*FCN*/geterrno()
  124. {
  125.     rbtype errval;
  126.  
  127.     ads_getvar(/*MSG0*/"ERRNO", &errval);
  128.  
  129.     return errval.resval.rint;
  130. }
  131.  
  132. /******************************************************************************/
  133. /*.doc funcload(internal) */
  134. /*+
  135.     This function is called to define all function names in the ADS
  136.     function table.  Each named function will be callable from lisp or
  137.     invokable from another ADS application.
  138. -*/
  139. /******************************************************************************/
  140. int
  141. /*FCN*/funcload()
  142. {
  143.     int i;
  144.     for (i = 0; i < ELEMENTS(exfun); i++) {
  145.         if (ads_defun(exfun[i].name, i) != RTNORM)
  146.             return RTERROR;
  147.     }
  148.  
  149.     return RTNORM;
  150. }
  151.  
  152. /******************************************************************************/
  153. /*.doc funclunoad(internal) */
  154. /*+
  155.     This function is called to undefine all function names in the ADS
  156.     function table.  Each named function will be removed from the
  157.     AutoLISP hash table.
  158. -*/
  159. /******************************************************************************/
  160. int
  161. /*FCN*/funcunload()
  162. {
  163.     int i;
  164.  
  165.     /* Undefine each function we defined */
  166.  
  167.     for (i = 0; i < ELEMENTS(exfun); i++) {
  168.         ads_undef(exfun[i].name,i);
  169.     }
  170.  
  171.     return RTNORM;
  172. }
  173. /******************************************************************************/
  174. /*.doc dofun(internal) */
  175. /*+
  176.     This function is called to invoke the function which has the
  177.     registerd function code that is obtained from  ads_getfuncode.  The
  178.     function will return RTERROR if the function code is invalid, or
  179.     RSERR if the invoked function fails to return RTNORM.  The value
  180.     RSRSLT will be returned if the function code is valid and the
  181.     invoked subroutine returns RTNORM.
  182. -*/
  183. /******************************************************************************/
  184. int
  185. /*FCN*/dofun()
  186. {
  187.     int    val;
  188.     int    rc;
  189.  
  190.     ads_retvoid();
  191.  
  192.     if ((val = ads_getfuncode()) < 0 || val > ELEMENTS(exfun))
  193.         return RTERROR;
  194.  
  195.     rc = (*exfun[val].fptr)();
  196.  
  197.     return ((rc == RTNORM) ? RSRSLT:RSERR);
  198. }
  199.  
  200. /******************************************************************************/
  201. /*.doc main(internal) */
  202. /*+
  203.     This is the main entry point for the ADS application.  All ADS
  204.     requests will be dispatched from this function.  This is your
  205.     standard ADS dispatch loop.
  206. -*/
  207. /******************************************************************************/
  208. void
  209. /*FCN*/main(argc,argv)
  210. int argc;
  211. char *argv[];
  212. {
  213.     short scode = RSRSLT;          /* Normal result code (default) */
  214.     int   stat;
  215.     char errmsg[80];
  216.  
  217.     ads_init(argc, argv);          /* Initiate communication with AutoLISP */
  218.  
  219.     for ( ;; ) {                   /* Request/Result loop */
  220.  
  221.         if ((stat = ads_link(scode)) < 0) {
  222.             sprintf(errmsg,
  223.                     /*MSG1*/"GPALSYM: bad status from ads_link() = %d\n",
  224.                     stat);
  225. #ifdef Macintosh
  226.             macalert(errmsg);
  227. #else
  228.             puts(errmsg);
  229.             fflush(stdout);
  230. #endif /* Macintosh */
  231.             exit(1);
  232.         }
  233.  
  234.         scode = RSRSLT;           /* Reset result code */
  235.  
  236.         switch (stat) {
  237.  
  238.         case RQXLOAD:             /* Load & define functions */
  239.             scode = funcload() ? -RSRSLT : -RSERR;
  240.             break;
  241.  
  242.         case RQXUNLD:             /* Unload functions */
  243.             scode = funcunload() ? -RSRSLT : -RSERR;
  244.             ads_printf(/*MSG2*/"Unloading.\n");
  245.             break;
  246.  
  247.         case RQSUBR:             /* Handle request for external function */
  248.             dofun();
  249.             break;
  250.  
  251.         default:
  252.             break;
  253.         }
  254.     }
  255. }
  256.  
  257. /******************************************************************************/
  258. /*.doc getsym(internal) */
  259. /*+
  260.     This function is called from dofun function as a result of RQSUBR
  261.     request being sent to the main dispatch loop.  It requires one
  262.     string argument which specifies the name of an AutoLISP symbol
  263.     whos value should be returned.
  264.  
  265.     This function always returns RTNORM.  An error message will be
  266.     displayed in the event of an error, and nil will be returned.
  267.     Otherwise, the value of the AutoLISP symbol will be returned
  268.     to the AutoLISP caller.
  269. -*/
  270. /******************************************************************************/
  271. int
  272. /*FCN*/getsym()
  273. {
  274.     rbtype      *args;
  275.     int         rc;
  276.     rbtype      *varval = NULL;
  277.  
  278.     args = ads_getargs();
  279.     if (args == NULL || args->restype != RTSTR) {
  280.         ads_printf(/*MSG3*/"Requires an AutoLISP symbol name in \"s.\n");
  281.         return RTNORM;
  282.     }
  283.  
  284.     rc = ads_getsym(args->resval.rstring, &varval);
  285.     if (rc != RTNORM) {
  286.         switch(geterrno()) {
  287.         case OL_ESYMNAM:
  288.             ads_printf(/*MSG4*/"Invalid AutoLISP symbol name.\n");
  289.             break;
  290.         case OL_ENULLPTR:
  291.             ads_printf(/*MSG5*/"Passed NULL pointer to ads_getsym.\n");
  292.             break;
  293.         case OL_ENEWRB:
  294.             ads_printf(/*MSG6*/"Can't allocate result buffer.\n");
  295.             break;
  296.         case OL_ESYMVAL:
  297.             ads_printf(/*MSG7*/"Invalid symbol value.\n");
  298.             break;
  299.         }
  300.     } else {
  301.        if (varval != NULL) {
  302.            if (varval->rbnext == NULL)
  303.                ads_retval(varval);
  304.            else
  305.                ads_retlist(varval);
  306.        }
  307.     }
  308.     if (varval != NULL)
  309.         ads_relrb(varval);
  310.  
  311.     return RTNORM;
  312. }
  313.  
  314. /******************************************************************************/
  315. /*.doc putsym(internal) */
  316. /*+
  317.     This function is called from dofun function as a result of RQSUBR
  318.     request being sent to the main dispatch loop.  It requires two
  319.     arguments.  The first argument is a string containing the name of
  320.     an AutoLISP symbol to set.  The second argument can be any data
  321.     type or list containing any data type which can be represented
  322.     in an ADS resbuf structure.
  323.  
  324.     This function always returns RTNORM to the C caller.  This function
  325.     always return nil if the operation fails.  Otherwise, the value of
  326.     the second argument is returned.
  327.  
  328.     A message will be displayed if an error occurs, and the value
  329.     returned to the AutoLISP caller will be nil.
  330. -*/
  331. /******************************************************************************/
  332. int
  333. /*FCN*/putsym()
  334. {
  335.     rbtype      *args;
  336.     int         rc;
  337.  
  338.     args = ads_getargs();
  339.     ads_retnil();
  340.  
  341.     if (args == NULL || args->restype != RTSTR) {
  342.         ads_printf(/*MSG8*/"Requires name of AutoLISP symbol in \"s, and\n");
  343.         ads_printf(/*MSG9*/"value to be assigned to symbol.\n");
  344.     }
  345.     
  346.     rc = ads_putsym(args->resval.rstring, args->rbnext);
  347.     if (rc != RTNORM) {
  348.         switch(geterrno()) {
  349.         case OL_ESYMNAM:
  350.             ads_printf(/*MSG10*/"Invalid AutoLISP symbol name.\n");
  351.             break;
  352.         case OL_ESYMVAL:
  353.             ads_printf(/*MSG11*/"Invalid symbol value.\n");
  354.             break;
  355.         }
  356.     } else {
  357.         if (args->rbnext != NULL) {
  358.             if (args->rbnext->rbnext == NULL)
  359.                 ads_retval(args->rbnext);
  360.             else
  361.                 ads_retlist(args->rbnext);
  362.         }
  363.     }
  364.     return RTNORM;
  365. }
  366.  
  367.